package com.noble.glamour.ui.agora.act;

import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import com.noble.glamour.ui.agora.RtcUIShow;
import com.noble.library.log.PToast;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.observe.PObserver;
import com.noble.glamour.R;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.glamour.module.util.UIShow;
import com.noble.glamour.ui.agora.act.base.RtcBaseActivity;
import com.noble.glamour.ui.agora.act.bean.Accept;
import com.noble.glamour.ui.agora.act.voice.girlver.RtcInitFootPanel;
import com.noble.glamour.ui.agora.act.voice.girlver.RtcInitHeadPanel;
import com.noble.glamour.ui.agora.model.AgoraConstant;
import com.noble.glamour.ui.agora.util.RtcJointChannel;
import com.noble.glamour.ui.agora.util.RtcSubUtil;

import java.util.Map;

/**
 * 女性任务版： 女性群邀，单邀
 * <p>
 * Created by Su on 2017/6/20.
 */
public class RtcGroupInitAct extends RtcBaseActivity implements PObserver, View.OnClickListener {
    private boolean isRelease = true; // 资源释放

    private RtcInitHeadPanel headPanel;
    private RtcInitFootPanel footPanel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.f1_chat_group_wait);
        initView();
    }

    private void initView() {
        findViewById(R.id.iv_shrink_narrow).setOnClickListener(this);

        // 头部Panel
        RelativeLayout header = (RelativeLayout) findViewById(R.id.ll_head_panel);
        headPanel = new RtcInitHeadPanel(this);
        header.addView(headPanel.getContentView());

        // 底部Panel
        RelativeLayout footer = (RelativeLayout) findViewById(R.id.ll_foot_panel);
        footPanel = new RtcInitFootPanel(this);
        footer.addView(footPanel.getContentView());

        RtcSubUtil.getInstance().playSound();  // 开启铃声
        MsgMgr.getInstance().attach(this);
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_Rtc_Chat_Status:
                Map<String, Object> data = (Map<String, Object>) value;
                switch ((int) data.get("type")) {
                    case 2:     // 同意加入
                        startChat();
                        break;

                    case 3:     // 拒絕加入
                    case 4:     // 挂断
                    default:
                        finish();
                        break;
                }
                break;

            // 女性邀请状态收到男方音视频邀请
            case MsgType.MT_Rtc_Group_Chat_Msg:
                ModuleMgr.getRtcChatMgr().reqAcceptVideoChat(config().mVcid, new RequestComplete() {
                    @Override
                    public void onRequestComplete(HttpResponse response) {
                        if (response.isOk()) {
                            Accept accept = (Accept) response.getBaseData();
                            config().mChannelKey = accept.getChannelKey();
                            config().msgVer = accept.getMsgVer();
                            startChat();
                            return;
                        }
                        PToast.showShort(response.getMsg() == null ? getString(R.string.chat_join_fail_tips) : response.getMsg());
                        ModuleMgr.getRtcEnginMgr().reqVCGroupCancel(config().mInviteID, null);
                        finish();
                    }
                });
                break;

            case MsgType.MT_Rtc_Group_Chat_invite:
                footPanel.refreshInviteNum((int) value);
                break;

            case MsgType.MT_Rtc_Chat_Timer_Msg:
                if ((int) value == AgoraConstant.TIMER_INVITE)
                    footPanel.refreshHangUp();
                break;
        }
    }

    private void startChat() {
        isRelease = false;
        RtcJointChannel.getInstance().joinChannelPrepare(true);
        if (config().mChatType == AgoraConstant.RTC_CHAT_VIDEO)
            RtcUIShow.showRtcVideoActFinish(RtcGroupInitAct.this);
        else
            RtcUIShow.showRtcVoiceActFinish(RtcGroupInitAct.this);
    }

    @Override
    public void finish() {
        super.finish();
        MsgMgr.getInstance().detach(this);
        RtcSubUtil.getInstance().stopPlay();

        // 处于悬浮窗、跳下个页面， 则只需释放本地Timer计时
        if (RtcJointChannel.getInstance().getFloatStatus() || !isRelease) {
            ModuleMgr.getRtcEnginMgr().releaseLocalTimer();
            return;
        }

        // 全部回收
        ModuleMgr.getRtcEnginMgr().releaseAll();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_shrink_narrow:
                if (checkOverLaysPermission())
                    ModuleMgr.getRtcEnginMgr().startRtcFloatService();
                break;
        }
    }
}
